//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package Snake;

import java.util.LinkedList;

public class Snake {
    int direction = 37;
    LinkedList<Grid> body = new LinkedList();

    public Snake() {
        this.body.add(new Grid(400, 101));
        this.body.add(new Grid(400, 91));
        this.body.add(new Grid(400, 81));
        this.body.add(new Grid(400, 71));
        this.body.add(new Grid(400, 61));
        this.body.add(new Grid(400, 51));
    }

    public int getDirection() {
        return this.direction;
    }

    public void setDirection(int direction) {
        if (this.direction != 37 || direction != 39) {
            if (this.direction != 39 || direction != 37) {
                if (this.direction != 40 || direction != 38) {
                    if (this.direction != 38 || direction != 40) {
                        this.direction = direction;
                    }
                }
            }
        }
    }

    public LinkedList<Grid> getBody() {
        return this.body;
    }

    public void setBody(LinkedList<Grid> body) {
        this.body = body;
    }

    public Grid getHead() {
        return (Grid)this.body.get(0);
    }

    public void move() {
        this.body.removeLast();
        Grid head = this.getHead();
        Grid grid = null;
        switch (this.direction) {
            case 37 -> grid = new Grid(head.getX() - 10, head.getY());
            case 38 -> grid = new Grid(head.getX(), head.getY() - 10);
            case 39 -> grid = new Grid(head.getX() + 10, head.getY());
            case 40 -> grid = new Grid(head.getX(), head.getY() + 10);
        }

        this.body.addFirst(grid);
    }

    public boolean eat(Grid food) {
        Grid head = this.getHead();
        return head.getX() == food.getX() && head.getY() == food.getY();
    }

    public boolean isAlive() {
        Grid head = this.getHead();
        int hX = head.getX();
        int hY = head.getY();
        if (hX >= 0 && hX < 580 && hY >= 1 && hY < 361) {
            for(int i = 3; i < this.body.size(); ++i) {
                Grid grid = (Grid)this.body.get(i);
                if (hX == grid.getX() && hY == grid.getY()) {
                    return false;
                }
            }

            return true;
        } else {
            System.out.println("撞墙");
            return false;
        }
    }
}
